const app = getApp()

function fetch(options) {
  if (options.load == 1) {
    wx.showLoading({
      mask: true,
      title: '加载中'
    })
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: options.url,
      method: options.method,
      data: options.data,
      header: {
        'Content-Type': 'application/json',
        'token': app.globalData.token
      },
      success: function (res) {
          // console.log(res,'验证code');
        options.load == 1 ? wx.hideLoading() : '';
        if (res.data.code == 11111) {
          wx.showToast({
            title: '身份过期，请重新登录',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1000)
          wx.clearStorageSync()
          clearGlobalData()
          return
        } else if (res.data.code == 11112) {
          wx.showToast({
            title: '身份过期，请重新登录',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1000)
          wx.clearStorageSync()
          clearGlobalData()
          return
        }  else if (res.data.code == 11113) {
          wx.showToast({
            title: '登录过期，请重新登录',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1000)
          wx.clearStorageSync()
          clearGlobalData()
          return
        } else if (res.data.code == 11114) {
          wx.showToast({
            title: '账号已在另一台设备登录',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1000)
          wx.clearStorageSync()
          clearGlobalData()
          return
        }else if (res.data.code == 11115) {
            wx.showToast({
              title: '账号已在另一台设备登录',
              icon: 'none',
              duration: 2000
            })
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }, 1000)
            wx.clearStorageSync()
            clearGlobalData()
            return
          }else if (res.data.code == 10030) {
            wx.showToast({
              title: '用户异常，请重新登录',
              icon: 'none',
              duration: 2000
            })
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }, 1000)
            wx.clearStorageSync()
            clearGlobalData()
            return
          } 
        resolve(res.data)
      },
      fail: function (res) {
        options.load == 1 ? wx.hideLoading() : '';
        wx.showToast({
          title: '网络超时，请重试',
          icon: 'none',
          duration: 2000
        })
        reject(res)
      }
    })
  })
}

/**
 * @name: 清除app.globalData
 * @msg: 
 * @return {*}
 */
function clearGlobalData() {
  app.globalData.userId = app.globalData.token = app.globalData.mobile = app.globalData.nickname = app.globalData.username = app.globalData.wxAvatar = app.globalData.openId = app.globalData.wxNickName = app.globalData.tenantId = app.globalData.orgId = app.globalData.orgName = app.globalData.rolename = app.globalData.descriptionRole = ''
  app.globalData.isLogin = false
}

//得到一个请的token   --  暂时不用
function getNewToken() {
  let that = this;
  return new Promise((resolve, reject) => {
    wx.login({
      success(res) {
        let code = res.code;
        wx.request({
          url: app.globalData.reqUrl + '/user/login', //在user/login的接口中，后台返回用户的信息并且带有token
          data: {
            mobile: phone,
            password: password
          },
          success(res) {
            if (res.data.code == 20000) { //这里相当于用户又一次登录，并且得到了一个新的token 并把它写入了 storage本地缓存中
              app.globalData.token = rows.token
              wx.setStorageSync('token', rows.token);
              resolve(); //通过Promise的 resolve(),把返回的结果暴露出去  这个暴露好像用处不太大， 可以不用
            }
          },
          fail(err) {
            console.log(err)
          }
        })
      }
    });
  })
}

module.exports = {
  fetch
}